"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccountDao = void 0;
const DbConfig_1 = require("../../src/config/DbConfig");
const DaoBase_1 = require("../../src/engine/dao/DaoBase");
const Base64Helper_1 = require("../../src/engine/utils/Base64Helper");
const DateUtil_1 = require("../../src/engine/utils/DateUtil");
const Logger_1 = require("../../src/engine/utils/Logger");
const MysqlManager_1 = require("../../src/engine/utils/MysqlManager");
class AccountDao extends DaoBase_1.DaoBase {
    static getDbName() {
        // if (Config.environment == Config.EnvironmentType.yueyouhui) {
        //     return DbConfig.Db_Name.db_account2
        // } 
        // else if(Config.environment == Config.EnvironmentType.xj){
        //     return DbConfig.Db_Name.db_account2
        // }
        // else if(Config.environment == Config.EnvironmentType.Jk){
        //     return DbConfig.Db_Name.db_account3
        // }
        // else {
        return DbConfig_1.DbConfig.Db_Name.db_account;
        // }
    }
    static is_account_exist(account) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getDbName());
            let sql = "select account from " + this.getDbName() + "." + this.Tb_Name + " where `account`=?;";
            let sqlParams = [account];
            let results = yield client.queryPromise(sql, sqlParams);
            // Logger.log("is_account_exist", results)
            if (results && results.length > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static get_account_info(account) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getDbName());
            let sql = "select * from " + this.getDbName() + "." + this.Tb_Name + " where `account`=?;";
            let sqlParams = [account];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results.length > 0) {
                results[0]["nick_name"] = Base64Helper_1.Base64Helper.decode(results[0]["nick_name"]);
                Logger_1.Logger.log("get_account_info", results);
                return results[0];
            }
            else {
                return null;
            }
        });
    }
    static create_account(account, password, account_type, token, status, ip, nick_name, channel, os, phone_type, bundle_name, system_version, sex = 1, headimgurl = "") {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getDbName());
            nick_name = Base64Helper_1.Base64Helper.encode(nick_name);
            // let sql: string = "select * from " + this.DB_NAME + "." + this.Tb_Name + " where `account`=?;"
            let sql = 'insert INTO t_accounts(account,password,account_type, token, status, ip, nick_name, channel,os,phone_type, bundle_name, system_version, create_time, last_login_time, sex, headimgurl) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ;';
            let create_time = DateUtil_1.default.formatDateStr(DateUtil_1.default.now());
            let sqlParams = [account, password, account_type, token, status, ip, nick_name, channel, os, phone_type, bundle_name, system_version, create_time, create_time, sex, headimgurl];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("create_account", results);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static update_login(account, token, ip, nick_name, channel, os, phone_type, bundle_name, system_version, sex = 1, headimgurl = "") {
        return __awaiter(this, void 0, void 0, function* () {
            let sql = "update " + this.getDbName() + "." + this.Tb_Name + " set `token`=?, `last_login_time`=?, `ip`=?, `nick_name`=?, `channel`=?, `os`=?, `phone_type`=?, `bundle_name`=?, `system_version`=?, `sex`=?, `headimgurl`=?   where `account`=?";
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getDbName());
            nick_name = Base64Helper_1.Base64Helper.encode(nick_name);
            let last_login_time = DateUtil_1.default.formatDateStr(DateUtil_1.default.now());
            let sqlParams = [token, last_login_time, ip, nick_name, channel, os, phone_type, bundle_name, system_version,
                sex, headimgurl, account];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("update_login=", sql, sqlParams, results);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static reset_pass(phone, password) {
        return __awaiter(this, void 0, void 0, function* () {
            let sql = "update " + this.getDbName() + "." + this.Tb_Name + " set `password`=?  where `phone`=?";
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getDbName());
            let sqlParams = [password, phone];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("reset_pass=", sql, sqlParams, results);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static bindPhone(account, phone, pass) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getDbName());
            let sql = "update " + this.getDbName() + "." + this.Tb_Name + " set `phone`=?, `password`=?  where `account`=?";
            let sqlParams = [phone, pass, account];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("bindPhone=", sql, sqlParams, results);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static getAccountByPhone(phone) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getDbName());
            let sql = "select * from " + this.getDbName() + "." + this.Tb_Name + " where `phone`=?;";
            let sqlParams = [phone];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results.length > 0) {
                results[0]["nick_name"] = Base64Helper_1.Base64Helper.decode(results[0]["nick_name"]);
                Logger_1.Logger.log("getAccountByPhone", results);
                return results[0];
            }
            else {
                return null;
            }
        });
    }
}
exports.AccountDao = AccountDao;
AccountDao.DB_NAME = DbConfig_1.DbConfig.Db_Name.db_account;
AccountDao.Tb_Name = "t_accounts";
